package main

import (
	"fmt"
	"go_patterns/AbstractFactory"
	"go_patterns/StrategyPattern"
	"net/http"
)

func main() {
	//Person := AbstractFactory.NewPerson("zs", 25)
	//Person.Greet()
	//doer := AbstractFactory.NewMockHttpClient()
	//err := QueryUser(doer)
	//if err != nil {
	//	fmt.Printf("Error:%v", err)
	//}

	/*工厂方法模式*******************************************/
	//newBaby := FactoryMethod.NewPersonFactory(12)
	//tom := newBaby("tom")
	//zs := newBaby("zs")
	//fmt.Println(tom)
	//fmt.Println(zs)

	/*策略模式***********************************************/
	operator := StrategyPattern.Operator{}
	operator.SetOperator(&StrategyPattern.Reduce{})
	result := operator.Calculate(1, 2)
	fmt.Printf("reduce:%d\n", result)
	//通过对执行者对象设置不同的执行策略，使其执行不同的方法
	operator1 := StrategyPattern.Operator{}
	operator1.SetOperator(&StrategyPattern.Add{})
	result1 := operator1.Calculate(1, 2)
	fmt.Printf("add:%d\n", result1)
}

func QueryUser(doer AbstractFactory.Doer) error {
	req, err := http.NewRequest("Get", "https://www.52lhg.org", nil)
	if err != nil {
		return err
	}
	res, err := doer.Do(req)
	if err != nil {
		return err
	}
	fmt.Println(res)
	return nil
}
